package com.arvin.skin.core.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;

public class SkinResources {
    private static SkinResources instance;

    private SkinResources(Context context) {
        mAppResources = context.getResources();
    }

    private Resources mSkinResources;
    private String mSkinPkgName; // 皮肤包 包名
    private  boolean isDefaultSkin = true;
    private Resources mAppResources;

    public static void init(Context context){
        if (instance == null) {
            synchronized (SkinResources.class){
                if (instance == null) {
                    instance = new SkinResources(context);
                }
            }
        }
    }

    public static SkinResources getInstance() {
        return instance;
    }

    public void reset(){
        mSkinPkgName = "";
        mSkinResources = null;
        isDefaultSkin = true;
    }

    public void applySkin(Resources resources,String pkgName){
        mSkinResources = resources;
        mSkinPkgName = pkgName;
        isDefaultSkin = TextUtils.isEmpty(pkgName) || resources == null;

    }

    public int getIdentifier(int resId){
        if (isDefaultSkin){
            return resId;
        }
        String resName = mAppResources.getResourceName(resId);
        if (resName.contains("/")){
            int index = resName.lastIndexOf("/");
            resName = resName.substring(index+1);
        }
        String resType = mAppResources.getResourceTypeName(resId);
        int skinResId = mSkinResources.getIdentifier(resName,resType,mSkinPkgName);
        return skinResId;
    }

    public int getColor(int resId){
        if (isDefaultSkin){
            return mAppResources.getColor(resId);
        }
        int skinId = getIdentifier(resId);
        if (skinId==0){
            return mAppResources.getColor(resId);
        }

        return mSkinResources.getColor(skinId);
    }

    public ColorStateList getColorStateList(int resId){
        if (isDefaultSkin){
            return mAppResources.getColorStateList(resId);
        }
        int skinId = getIdentifier(resId);
        if (skinId==0){
            return mAppResources.getColorStateList(resId);
        }

        return mSkinResources.getColorStateList(skinId);
    }

    public Drawable getDrawable(int resId){
        if (isDefaultSkin){
            return mAppResources.getDrawable(resId);
        }
        int skinId = getIdentifier(resId);
        if (skinId==0){
            return mAppResources.getDrawable(resId);
        }

        return mSkinResources.getDrawable(skinId);
    }

    public Object getBackground(int resId){
        String resourceTypeName = mAppResources.getResourceTypeName(resId);
        if (resourceTypeName.equals("color")){
            return getColor(resId);
        }else {
            return getDrawable(resId);
        }
    }

    public String getString(int resId){
        if (isDefaultSkin){
            return mAppResources.getString(resId);
        }
        int skinId = getIdentifier(resId);
        if (skinId==0){
            return mAppResources.getString(resId);
        }

        return mSkinResources.getString(skinId);
    }

    public Typeface getTypeface(int resId){
        String skinTypefacePath = getString(resId);
        if (TextUtils.isEmpty(skinTypefacePath)){
            return Typeface.DEFAULT;
        }
        try {
            Typeface typeface;
            if (isDefaultSkin){
                typeface = Typeface.createFromAsset(mAppResources.getAssets(),skinTypefacePath);
                return typeface;
            }
            typeface = Typeface.createFromAsset(mSkinResources.getAssets(),skinTypefacePath);
            return typeface;
        }catch (Exception e){
            e.printStackTrace();
        }
        return Typeface.DEFAULT;
    }
}
